#include "playscene.h"
#include<QMenuBar>
#include<QPainter>
#include"mypushbutton.h"
#include<QTimer>
#include<QLabel>
#include"mycoin.h"
#include"dataconfig.h"
#include<QDebug>
#include<QPropertyAnimation>
#include<QSound>
//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{

//}
PlayScene::PlayScene(int num)
{
    this->LevelIndex=num;

    //设置选择关卡场景
    this->setFixedSize(320,580);

    //设置图标
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //设置标题
    this->setWindowTitle("选择关卡场景");
    //创建菜单栏
    QMenuBar *bar=menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu *startMenu=bar->addMenu("开始");

    QAction *quitAction=startMenu->addAction(tr("退出(&q)"));
    quitAction->setShortcut(QKeySequence("Ctrl+q"));
    //图标
    quitAction->setIcon(QIcon(":/res/Coin0001.png"));

    //点击退出实现退出游戏
    //点击退出实现退出游戏

    connect(quitAction,&QAction::triggered,[=](){

        this->close();
    });

    //返回按钮
    //返回按钮
    MyPushButton *backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    //点击返回
    //添加音效
    QSound *chooseSound=new QSound(":/res/TapButtonSound.wav",this);
    connect(backBtn,&QPushButton::clicked,[=](){
        chooseSound->play();
       // qDebug()<<"点击了返回";
        //主场景监听ChooseLevelScene的返回按键
        QTimer::singleShot(500,[=](){
            emit chooseSceneBack();//发送点击了返回的按钮
        });

    });

    //显示当前的关卡
    QLabel *LevelLabel=new QLabel(this);
    QString str=QString("Leavel:%1").arg(QString::number(this->LevelIndex));

    //设置字体、字号
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);

    //将字体设置到标签控件里
    LevelLabel->setFont(font);
    LevelLabel->setText(str);

    //设置大小和位置
    LevelLabel->setGeometry(QRect(30,this->height()-50,130,50));

    //初始化每个关卡的二维数组
    dataConfig dcf;
    for(int i=0;i<4;i++)
    {

        for (int j=0;j<4 ;j++ )
        {
           this->gameArray[i][j]=dcf.mData[this->LevelIndex][i][j];

        }
    }

    QLabel *winLabel=new QLabel(this);
    QPixmap temPix;
    temPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,temPix.width(),temPix.height());
    winLabel->setPixmap(temPix);
    winLabel->move((this->width()-temPix.width())*0.5,-temPix.height()) ;//设置负数 是为了显示动画


    //显示金币背景图案
    for(int i=0;i<4;i++)
    {
        for (int j=0;j<4 ;j++ )
        {
            //绘制背景图案
            QPixmap pix=QPixmap(":/res/BoardNode.png");
            QLabel *label=new QLabel(this);
            label->setPixmap(pix);
            label->setGeometry(0,0,pix.width(),pix.height());
            label->move(57+i*pix.width(),200+j*pix.height());

            //创建金币
             QString co;
            if( this->gameArray[i][j]==1)
            {
                 co=":/res/Coin0001.png";
            }
            else
                 co=":/res/Coin0008.png";
            MyCoin *coin=new MyCoin(co);
            coin->setParent(this);
            coin->move(59+i*pix.width(),204+j*pix.height());

            //给金币属性赋值
            coin->posX=i;
            coin->posY=j;
            coin->flag=this->gameArray[i][j];//1正面 0反面

            //将金币放入到金币的二维数组里以便以后期的维护

            coinBtn[i][j]=coin;

            //点击金币 进行翻转
            connect(coin,&MyCoin::clicked,[=](){
                //在翻转金币的时候禁用其他按钮
                for(int i = 0 ; i < 4; i++)
                {
                    for(int j = 0 ; j < 4 ; j++)
                    {
                        this->coinBtn[i][j]->isWin = true;
                    }
                }

                coin->changeFlag();
                //翻金币音效
                QSound *flipSound=new QSound(":/res/ConFlipSound.wav",this);
                flipSound->play();
                //更新维护的数组
                this->gameArray[i][j]=this->gameArray[i][j]==0 ? 1:0;

                //翻转周围的金币
                //延时翻转
                QTimer::singleShot(300,this,[=](){

                    if(coin->posX+1<4)//周围的右侧金币翻转的条件
                    {
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY]=this->gameArray[coin->posX+1][coin->posY]==0 ? 1:0;

                    }
                    if(coin->posX-1>-1)//周围的左侧金币翻转的条件
                    {
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX-1][coin->posY]=this->gameArray[coin->posX-1][coin->posY]==0 ? 1:0;

                    }
                    if(coin->posY-1>-1)//周围的上侧金币翻转的条件
                    {
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY-1]=this->gameArray[coin->posX][coin->posY-1]==0 ? 1:0;

                    }
                    if(coin->posY+1<4)//周围的下侧金币翻转的条件
                    {
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY+1]=this->gameArray[coin->posX][coin->posY+1]==0 ? 1:0;

                    }


                    //在翻转完金币的时候释放其他按钮
                    for(int i = 0 ; i < 4; i++)
                    {
                        for(int j = 0 ; j < 4 ; j++)
                        {
                            this->coinBtn[i][j]->isWin = false;
                        }
                    }
                    //判断是否胜利的标志
                    this->isWin=true;
                    for(int i=0;i<4;i++)
                    {
                        for (int j=0;j<4 ;j++ )
                        {
                            if(coinBtn[i][j]->flag==false)//有一个是银币就失败
                            {
                                this->isWin=false;
                                break;
                            }
                        }
                    }

                    if(this->isWin==true)
                    {
                        for(int i=0;i<4;i++)
                        {
                            for (int j=0;j<4 ;j++ )
                            {
                                //将所有的按键胜利标志改为true
                                //点击按钮属于鼠标的事件  只需要在鼠标事件设置就可以控制 所有的金币不能点击
                              coinBtn[i][j]->isWin=true;
                            }
                        }

                            //延时效果

                            QTimer::singleShot(500,winLabel,[=](){
                                //胜利音效
                                QSound *winSound=new QSound(":/res/LevelWinSound.wav",this);
                                winSound->play();

                                //将胜利图片砸下来
                                QPropertyAnimation *animation=new QPropertyAnimation(winLabel,"geometry");
                                //设置动画间隔
                                animation->setDuration(1000);
                                //起始位置
                                animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                                //结束位置
                                animation->setEndValue(QRect(winLabel->x(),winLabel->y()+temPix.height()*2,winLabel->width(),winLabel->height()));
                                //设置弹跳曲线
                                animation->setEasingCurve(QEasingCurve::OutBounce);
                                animation->start();
                            });


                    }
                });
            });
        }
    }


}
void PlayScene::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //添加标题
    pix.load(":/res/Title.png");
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);
    //painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);


}
